import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
import NutUI from '@nutui/nutui';
import '@nutui/nutui/dist/nutui.css';
import ShopingCar from "../views/ShopingCar";
import Login from "../views/Login";
import Register from "../views/Register";
import CreateOrder from "../views/CreateOrder";
import AddressManage from "../views/AddressManage.vue";
import Order from "../views/Order";
import SuccessPay from "../views/SuccessPay";
import Home from "../views/Home";
import Active from "../views/Active";

NutUI.install(Vue);
Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'index',
        component: index
    },
    {
        path: '/Login',
        name: 'Login',
        component: Login
    },
    {
        path: '/CreateOrder',
        name: 'CreateOrder',
        component: CreateOrder
    },
    {
        path: '/order',
        name: 'Order',
        component: Order
    },
    {
        path: '/successpay',
        name: 'SuccessPay',
        component: SuccessPay
    },
    {
        path: '/Register',
        name: 'Register',
        component: Register
    },
    {
        path: '/Home',
        name: 'Home',
        component: Home
    },
    {
        path: '/active',
        name: 'Active',
        component: Active
    },
    {
        path: '/shopingCar',
        name: 'ShopingCar',
        component: function () {
            return import( '../views/ShopingCar.vue')
        }
    },
    {
        path: '/lala',
        name: 'lala',

        component: function () {
            return import( '../views/lala.vue')
        }
    },
    {
        path: '/goodstype',
        name: 'goodstype',

        component: function () {
            return import( '../views/goodstype.vue')
        }
    },
    {
        path: '/me',
        name: 'me',
        component: function () {
            return import( '../views/me.vue')
        }
    },
    {
        path: '/search',
        name: 'search',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: function () {
            return import(/* webpackChunkName: "about" */ '../views/search.vue')
        }
    },
    {
        path: '/goodsinfo',
        name: 'goodsinfo',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: function () {
            return import(/* webpackChunkName: "about" */ '../views/goodsinfo.vue')
        }
    },
    {
        path: '/addressmanage',
        name: 'AddressMange',
        component: AddressManage
    }
]

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})
router.beforeEach((to, from, next) => {

    if(to.name=="Login"||to.name=="Register"||to.name=="index"||to.name=="goodstype"||to.name=="search"||to.name=="goodsinfo")
    {
        next();
    }else
    {
    let token = $cookies.get("token");
    if ( !token && typeof(token)!="undefined" && token!=0) {
        next('/Login');
    } else {
        next();
    }
    }
});
export default router
